package com.selenium.core.functionlibrary;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.htmlparser.jericho.Element;
import net.htmlparser.jericho.HTMLElementName;
import net.htmlparser.jericho.Source;

/**
 * This class is deprecated, use <code>HTMLTableParser</code> for same
 * @author tbhadauria
 *
 */
@Deprecated
public class HTMLTableParserOld {

	/**
	 * Returns the <code>Segment</code> or content of HTML table
	 * available between Start and End tag
	 * 
	 * @param tableSource
	 * @param rawHTMLData 
	 * 
	 * @return HTML Table data
	 */
	public static List<String> getTableData(Source tableSource, Boolean rawHTMLData) {

		// Table data to be returned
		List<String> tableData = new ArrayList<String> ();

		// Collect table rows
		List<Element> tableRows = tableSource.getAllElements(HTMLElementName.TR);		

		// Loop through table rows
		for (int tableRowIndex=0; tableRowIndex<tableRows.size(); tableRowIndex++) {

			Source tableRow = new Source(tableRows.get(tableRowIndex));		
			List <Element> data = tableRow.getAllElements(HTMLElementName.TD);

			// Loop through table columns
			for(int tableColummnIndex=0; tableColummnIndex<data.size();
			tableColummnIndex++) {

				if(rawHTMLData == false) {

					tableData.add(data.get(tableColummnIndex).getContent().getTextExtractor().toString());					
				} else {

					tableData.add(data.get(tableColummnIndex).getContent().toString());					
				}								
			}
		}		
		return tableData;
	}

	/**
	 * Returns number of rows in table
	 * 
	 * @param tableSource
	 * @return Number of table rows
	 */
	public static int countTableRows(Source tableSource) {

		List<Element> tableRows = tableSource.getAllElements(HTMLElementName.TR);		
		return tableRows.size();
	}

	/**
	 * Returns count of rows and corresponding count of columns in a HTML table
	 * 
	 * @param tableSource
	 * @return Map of Row and Column count
	 */
	public static Map<Integer, Integer> countTableColumnsInRows(Source tableSource) {

		Map<Integer, Integer> rowAndColumnCount = new HashMap<Integer, Integer>();

		// Collect table rows
		List<Element> tableRows = tableSource.getAllElements(HTMLElementName.TR);		

		// Loop through table rows
		for (int tableRowIndex=0; tableRowIndex<tableRows.size(); tableRowIndex++) {

			Source tableRow = new Source(tableRows.get(tableRowIndex));		
			List <Element> tableData = tableRow.getAllElements(HTMLElementName.TD);

			// Loop through table columns 
			// Loop would not initiate if there is no table data
			for(int tableColummnIndex=0; tableColummnIndex<tableData.size();
			tableColummnIndex++) {

				// If last column is reached
				if(tableColummnIndex == tableData.size()-1) {

					rowAndColumnCount.put(tableRowIndex, ++tableColummnIndex);													
				}
			}
		}					
		return rowAndColumnCount;		
	}

	/**
	 * 
	 * Retrieves table data for specific columns beginning from specific row
	 * To return data from beginning of row pass <code>rowNumber</code> as 0 
	 *  
	 * @param tableSource
	 * @param rawHTMLData
	 * @param rowNumber
	 * @param columnNumber
	 * @return Table Data
	 */
	public static List<String> getTableDataForColumn(Source tableSource, 
			Boolean rawHTMLData, int rowNumber, int columnNumber) {

		// Table data to be returned
		List<String> tableData = new ArrayList<String> ();

		// Collect table rows
		List<Element> tableRows = tableSource.getAllElements(HTMLElementName.TR);		

		// Loop through table rows
		for (int tableRowIndex=rowNumber; tableRowIndex<tableRows.size(); tableRowIndex++) {

			Source tableRow = new Source(tableRows.get(tableRowIndex));		
			List <Element> data = tableRow.getAllElements(HTMLElementName.TD);

			// If supplied index is with in size of table data
			// This check is useful when retrieving data from uneven html table
			if (columnNumber < data.size()) {
				// get data of specific column
				if(rawHTMLData == false) {
					tableData.add(data.get(columnNumber).getContent().getTextExtractor().toString());					
				} else {

					tableData.add(data.get(columnNumber).getContent().toString());					
				}
			}													
		}		
		return tableData;
	}
}
